#!/bin/bash
#TODO: Arguments to exe, find hostname & adjust directories (or change makefile)

LOCATION=2
TEST=1
USE_MPI=0

exe="prog"
script="prog"
exe_ext=""
PROJECT_DIR="_Prog"

CONFIGS=("SCHEDULE_TYPE" "USE_SIZE" "NUM_THREADS")
CONFIGS1=("static" "dynamic")
CONFIGS2=("0" "1")
CONFIGS3=("1" "2" "4" "8")
CONFIGS4=("")
LOOP1A=("")
LOOP1B=("")
LOOP2A=("")
LOOP2B=("")
LOOP3A=("")
LOOP3B=("")

if [ $LOCATION -eq 0 ]; then #Local
	cd ~/Projects/Source/${PROJECT_DIR}
	outdir="data/"
	exedir="bin/Linux64Mint11/Release/"
	datadir="../../Data/Matrices/MM/"
	#makedir="Make/"
elif [ $LOCATION -eq 1 ]; then #LionGA
	cd ~/${PROJECT_DIR}
	outdir="/gpfs/home/cjs1163/${PROJECT_DIR}/data/"
	exedir="/gpfs/home/cjs1163/${PROJECT_DIR}/bin/Linux64RHEL6.1/Release/"
	datadir="/gpfs/group/sscl/UFL_Matrices/MM/"
	#makedir="Make/"
else #Cyberstar
	cd ~/Graph
	outdir="/gpfs/home/cjs1163/${PROJECT_DIR}/data/"
	exedir="/gpfs/home/cjs1163/${PROJECT_DIR}/bin/Linux64RHEL5.8/Release/"
	datadir="/gpfs/group/sscl/UFL_Matrices/MM/"
	#makedir="Make/"
fi

echo ""
echo ""
echo "Job started on `hostname` at `date`"
echo ""
if [ ! -d ${outdir} ]; then
    mkdir ${outdir}
fi

#for i in ${!LOOP[*]};do
#for i in ${LOOP[@]}
#for (( i=i; i<${#LOOP[@]}; i++ ));do
#for (( i=0; i<4; i++ ));do
for c1 in ${!CONFIGS1[*]};do
for c2 in ${!CONFIGS2[*]};do
for c3 in ${!CONFIGS3[*]};do
for i1 in ${!LOOP1A[*]};do
for i2 in ${!LOOP2A[*]};do
for i3 in ${!LOOP3A[*]};do
	echo ""
	echo ""
	ext=""
	if [ "${exe_ext}" != "" ]; then
		ext="${ext}_${exe_ext}"
	fi
	if [ "${CONFIGS1[c1]}" != "" ]; then
		ext="${ext}_${CONFIGS1[c1]}"
	fi
	if [ "${CONFIGS2[c2]}" != "" ]; then
		ext="${ext}_${CONFIGS2[c2]}"
	fi
	if [ "${CONFIGS3[c3]}" != "" ]; then
		ext="${ext}_${CONFIGS3[c3]}"
	fi
	
	if [ "${LOOP1A[i]}" != "" ]; then
		ext=${ext}_${LOOP1A[i1]}
	fi
	if [ "${LOOP1B[i]}" != "" ]; then
		ext=${ext}_${LOOP1B[i1]}
	fi
	if [ "${LOOP2A[j]}" != "" ]; then
		ext=${ext}_${LOOP2A[i2]}
	fi
	if [ "${LOOP2B[j]}" != "" ]; then
		ext=${ext}_${LOOP2B[i2]}
	fi
	if [ "${LOOP3A[k]}" != "" ]; then
		ext=${ext}_${LOOP3A[i3]}
	fi
	if [ "${LOOP3B[k]}" != "" ]; then
		ext=${ext}_${LOOP3B[i3]}
	fi
	outfile="${outdir}${script}${ext}.dat"
	script_file="${script}${ext}.sh"
	new_exe="${exedir}${exe}${ext}"
	make_file="Makefile${ext}"

	rm -f ${make_file}
	if [ 0 -eq 1 ]; then
		sed -e "s|##EXE_SRC##|${exe}.cpp|g" \
		Makefile >> ${make_file}
	else
		cp Makefile ${make_file}
	fi

	if [ "${CONFIGS1[c1]}" != "" ]; then
		sed -e "s|##CONFIG1##|DEFINES += ${CONFIGS[0]}=${CONFIGS1[c1]}|g" ${make_file} >> make_temp
		mv make_temp ${make_file}
	fi
	if [ "${CONFIGS2[c2]}" != "" ]; then
		sed -e "s|##CONFIG2##|DEFINES += ${CONFIGS[1]}=${CONFIGS2[c2]}|g" ${make_file} >> make_temp
		mv make_temp ${make_file}
	fi
	if [ "${CONFIGS3[c3]}" != "" ]; then
		sed -e "s|##CONFIG3##|DEFINES += ${CONFIGS[2]}=${CONFIGS3[c3]}|g" ${make_file} >> make_temp
		mv make_temp ${make_file}
	fi
	if [ "${CONFIGS4[c4]}" != "" ]; then
		sed -e "s|##CONFIG3##|DEFINES += ${CONFIGS[3]}=${CONFIGS3[c4]}|g" ${make_file} >> make_temp
		mv make_temp ${make_file}
	fi

	if [ $LOCATION -eq 1 ]; then #LionGA
		sed -e "s|##LOCATION##|LOCATION = LionGA|g" ${make_file} >> make_temp
		mv make_temp ${make_file}
	else #Cyberstar
		sed -e "s|##LOCATION##|LOCATION = Cyberstar|g" ${make_file} >> make_temp
		mv make_temp ${make_file}
	fi
	make clean
	make -f ${make_file}
	rm -f ${exedir}${exe}.o
	rm -f ${new_exe}
	mv "${exedir}${exe}" "${new_exe}"

	rm -f ${script_file}
	echo "#!/bin/bash">>${script_file}
	if [ $LOCATION -eq 0 ]; then #Local
		echo "#PBS -l nodes=1:ppn=1">>${script_file}
	elif [ $LOCATION -eq 1 ]; then #LionGA
		echo "#PBS -l nodes=1:ppn=1:gpus=1">>${script_file}
	else #Cyberstar
		echo "#PBS -l nodes=1:ppn=1">>${script_file}
	fi
	echo "#PBS -l walltime=00:10:00">>${script_file}
	echo "#PBS -l pmem=4gb">>${script_file}
	echo "">>${script_file}
	echo "rm -f ${outfile}">>${script_file}
	#echo "rm -f ${summaryfile}">>${script_file}
	#echo "echo \"\"">>${script_file}
	#echo "echo \"${script_file} started on `hostname` at `date`\"">>${script_file}
	echo "echo \"Running ${new_exe} with arguments ${datadir} ${outfile}\"">>${script_file}
	if [ $USE_MPI -eq 1 ]; then
		if [ $LOCATION -eq 0 ]; then #Local
			echo "output=\$(mpirun -n 8 ${new_exe} ${datadir} ${outfile})">>${script_file}
		else #Cyberstar
			echo "output=\$(mpirun ${new_exe} ${datadir} ${outfile})">>${script_file}
		fi
	else
		echo "output=\$(${new_exe} ${datadir} ${outfile})">>${script_file}
	fi
	echo "echo \"\${output}\"">>${script_file}
	#echo "echo \"\${output}\">>${outfile}">>${script_file}
	#echo "echo \"\"">>${script_file}
	
	chmod 755 "${script_file}"
	if [ $TEST -eq 2 ]; then
		echo ""
	elif [ $TEST -eq 1 ]; then
		bash ${script_file}
		#mpirun -n 8 ${script_file}
		#mpirun -n 8 ${new_exe}
		echo ""
	else
		echo ""
		qsub ${script_file}
	fi
done
done
done
done
done
done

echo " "
echo "Job Ended at `date`"
echo " "
